package com.wzw.tcjh;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.wzw.util.UpdateUtil;
import com.wzw.util.UpdateXML;
import com.wzw.util.XMLUtil;


public class CustomerTools {
	
	public static List<Customer> getCustomerList(String searchCondition, PageDivision pd){
		String sql = null;
		SQLProcessor sp = null;
		List<Customer> customerList = new ArrayList<Customer>();
		try{
			sp = new SQLProcessor();
			sql = "select id, uname, phone, email, address, CONVERT(varchar(100), cdate, 23) as date, isreward, awardtime"
				+ " from customer where available = 1 "
				+ (searchCondition!=null?(" " + searchCondition) : "");
			List<Map<String, Object>> list = pd!=null?sp.executeQuery(sql, pd.getBegin(), pd.getPageSize()):sp.executeQuery(sql);
			for(int i=0; i<list.size(); i++){
				Customer customer = new Customer();
				Map<String, Object> customerMap = list.get(i);
				customer.setId((Integer)customerMap.get("id"));
				customer.setName((String)customerMap.get("uname"));
				customer.setPhone((String)customerMap.get("phone"));
				customer.setEmail((String)customerMap.get("email"));
				customer.setAddress((String)customerMap.get("address"));
				customer.setDate((String)customerMap.get("date"));
				customer.setAward(((Integer)customerMap.get("isreward"))==1?true:false);
				//customer.setAwardTime((String)customerMap.get("awardtime"));
				customerList.add(customer);
			}
			System.out.println("SQL: " + sql);
		}catch(SQLException se){
			System.out.println("Error SQL: " + sql);
			se.printStackTrace();
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return customerList;
	}
	
	public static boolean addCustomer(String uname, String phone, String email, String address, String pcode) throws Exception {
		return addCustomer(uname, phone, email, address, pcode, "0");
	}
	
	public static boolean addCustomer(String uname, String phone, String email, String address, String pcode, String from) throws Exception{
		SQLProcessor sp = null;
		String sql = null;
		try{
			sp = new SQLProcessor();
			sql = "insert into customer(uname, phone, email, address, cdate, isreward, projectcode, fromid) values(" 
				+ "'" + StrUtils.conv(uname).trim() + "','" 
				+ StrUtils.conv(phone).trim() + "','"
				+ StrUtils.conv(email).trim() + "','" 
				+ StrUtils.conv(address).trim() + "', getdate(), 0, '" + pcode + "','"
				+ from + "')";
			System.out.println(new Date());
			System.out.println(sql);
			sp.executeUpdate(sql);
		}catch(Exception se){
			sp.rollback();
			System.out.println(sql);
			se.printStackTrace();
			throw se;
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return true;
	}
	
	public static boolean removeAllCustomers() throws Exception{
		SQLProcessor sp = null;
		String sql = null;
		try{
			sp = new SQLProcessor();
			sql = "delete from customer";
			System.out.println(new Date());
			System.out.println(sql);
			sp.executeUpdate(sql);
		}catch(Exception se){
			sp.rollback();
			System.out.println(sql);
			se.printStackTrace();
			throw se;
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return true;
	}
	
	public static boolean updateAllAwardCustomers(String pcode) throws Exception{
		SQLProcessor sp = null;
		String sql = null;
		try{
			sp = new SQLProcessor();
			sql = "update customer set isreward = 0, awardtime = NULL where projectcode='" + pcode + "'";
			System.out.println(new Date());
			System.out.println(sql);
			sp.executeUpdate(sql);
		}catch(Exception se){
			sp.rollback();
			System.out.println(sql);
			se.printStackTrace();
			throw se;
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return true;
	}
	
	public static boolean updateCustomerAward(String[] ids, boolean isAward, String pcode) throws Exception{
		SQLProcessor sp = null;
		String sql = null;
		try{
			sp = new SQLProcessor();
			if(ids.length > 0){
				StringBuffer sb = new StringBuffer();
				for(String id : ids){
					sb.append(id + ",");
				}
				sql = "update customer set isreward = " + (isAward?"1":"0") + ", awardtime = getdate() where id in (" + sb.substring(0, sb.length()-1) + ")" +
						" and projectcode='" + pcode + "'";
				System.out.println(new Date());
				System.out.println(sql);
				sp.executeUpdate(sql);
			}
		}catch(Exception se){
			sp.rollback();
			System.out.println(sql);
			se.printStackTrace();
			throw se;
		}finally{
			SQLProcessor.staticClose(sp);
		}
		return true;
	}
	
	public static void writeAwardCustomerToXml(String projectCode, String webroot) {
		List<Customer> customerList = getCustomerList("and isreward = 1 and projectcode = '" + projectCode + "'", null);
		UpdateXML update = UpdateUtil.updateXML(projectCode);
		if(null != update) {
			update.updateAwardCustomer(customerList, webroot);
		}
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception{
//		addCustomer("大龟", "34232423423", "kokoko@163.com", "2");
//		updateAllAwardCustomers();
//		System.out.println(getCustomerList("order by date desc", null));
//		System.out.println();
		//removeAllCustomers();
	}

}
